"""输入组件类型，用于自定义创建组件执行流程"""
from pynput import mouse
from pynput import keyboard
from pynput.keyboard import Key
import contextlib
import time

"""
info_dict = {
    'button': mouse.Button.LEFT | mouse.Button.RIGHT | mouse.Button.MIDDLE,         按键类型
    'behavior':str                                                                  行为
    'count': int                                                                    点击次数
    'scroll': int                                                                   滚动步长
    'start':(x,y)                                                                   执行位置
    'end':(x,y)                                                                     结束位置
    'key':str                                                                       键盘按键
    'time':int                                                                      按键长按持续时间
    'key_string':str                                                                一串按键
    'key_group': [str]                                                              组合按键
}
"""
# 鼠标组件
class MouseComponent:
    def __init__(self,info_dict):
        self.typea = 'mouse'                                 # 组件自身标志
        self.info_dict = info_dict                           # 信息字典
        self.mouse_class = mouse.Controller()                # 鼠标控制对象

    def click(self):
        """点击"""
        self.mouse_class.position = self.info_dict['start']
        self.mouse_class.click(
            button=self.info_dict['button'],
            count=self.info_dict['count'],
        )
        print(f'鼠标点击{self.info_dict['start']}点击次数{self.info_dict['count']}')

    def drag(self):
        """拖拽"""
        start_x,start_y = self.info_dict['start']
        end_x,end_y = self.info_dict['end']

        # 将鼠标移动到起始位置
        self.mouse_class.position = (start_x,start_y)
        print(f'鼠标移动到起始位置{start_x},{start_y}')
        
        # 按下鼠标按钮
        self.mouse_class.press(button=self.info_dict['button'])
        print(f'鼠标在{start_x},{start_y}按下{self.info_dict["button"]}')
        
        # 添加小延迟确保按下动作被识别
        time.sleep(1)
        
        # 平滑移动鼠标到结束位置
        steps = 10  # 移动步数
        delta_x = (end_x - start_x) / steps
        delta_y = (end_y - start_y) / steps
        
        for i in range(steps):
            new_x = start_x + delta_x * (i + 1)
            new_y = start_y + delta_y * (i + 1)
            self.mouse_class.position = (new_x, new_y)
            time.sleep(0.01)  # 每步之间的小延迟
            
        # 释放鼠标按钮
        self.mouse_class.release(button=self.info_dict['button'])
        print(f'鼠标在{end_x},{end_y}释放{self.info_dict["button"]}')

    def roll(self):
        """滚动"""
        dx,dy = self.info_dict['scroll']
        self.mouse_class.scroll(dx=dx,dy=dy)
        print(f'滚轮滚动{self.info_dict['scroll']}')

# 键盘组件
class KeyboardComponent:
    def __init__(self,info_dict):
        self.typea = 'keyboard'                              # 组件自身标志
        self.info_dict = info_dict                           # 信息字典
        self.keyboard_class = keyboard.Controller()          # 键盘控制对象

    def tap(self):
        """点击一个按键"""
        self.keyboard_class.tap(self.info_dict['key'])
        print(f'按键点击{self.info_dict["key"]}')

    def strand(self):
        """输入一串字符"""
        self.keyboard_class.type(self.info_dict['key_string'])
        print(f'依次按下{self.info_dict["key_string"]}')

    def press(self):
        """长按一个字符"""
        self.keyboard_class.press(self.info_dict['key'])
        print(f'按键{self.info_dict["key"]}按下')
        time.sleep(self.info_dict['time'])
        print(f'等待{self.info_dict["time"]}秒')
        self.keyboard_class.release(self.info_dict['key'])
        print(f'按键{self.info_dict["key"]}释放')

    def key_group(self):
        """组合按键"""
        if not self.info_dict['key_group']:
            print('没有组合按键')
            return
        action_key = self.info_dict['key_group'][-1]         # 获取动作键
        modifier_keys = self.info_dict['key_group'][:-1]     # 获取修饰键

        # 开始组合键
        with contextlib.ExitStack() as stack:
            for modifier_key in modifier_keys:
                stack.enter_context(
                    self.keyboard_class.pressed(modifier_key)
                )
            self.keyboard_class.tap(action_key)
        eys_string = '+'.join([str(k) for k in self.info_dict['key_group']])
        print(f'组合建{eys_string}完成')

if __name__ == '__main__':
    dict_cs = {
        'button': mouse.Button.left,
        'count': None,
        'scroll': None,
        'start': (682,24),
        'end': (900,56),
        'key': 'a',
        'time': 2,
        'key_string': None,
        'key_group': [Key.ctrl,Key.space],
    }
    # 鼠标组件测试
    mouse = MouseComponent(info_dict=dict_cs)
    mouse.drag()
    # # 键盘组件测试
    # key = KeyboardComponent(info_dict=dict_cs)
    # key.key_group()
